﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace OrganizeYourSearch
{
	/// <summary>
	/// Interaction logic for WebBrowserControl.xaml
	/// </summary>
	public partial class WebBrowserControl : UserControl
	{
		public WebBrowserControl()
		{
			this.InitializeComponent();

            this.Loaded += new RoutedEventHandler(WebBrowserControl_Loaded);
            tbURLbox.KeyDown += new KeyEventHandler(tbURLbox_KeyDown);
            wbBrowser.Navigated += new NavigatedEventHandler(wbBrowser_Navigated);
       
            
		}
        void WebBrowserControl_Loaded(object sender, RoutedEventArgs e)
        {
            Keyboard.Focus(tbURLbox);
            // wbBrowser.Navigate(new Uri("about:blank")); 
            
        }

        void wbBrowser_Navigated(object sender, NavigationEventArgs e)
        {
            tbURLbox.Text = e.Uri.OriginalString;
        }




        

        void tbURLbox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                try
                {
                    string url = "http://";
                    if (!tbURLbox.Text.StartsWith("http://"))
                        url = url + tbURLbox.Text;
                    else
                        url = tbURLbox.Text;
                    Uri ui = new Uri(url.Trim(), UriKind.RelativeOrAbsolute);

                    wbBrowser.Navigate(ui);
                }
                catch
                {
                    
                    wbBrowser.Navigate(new Uri("about:navigationfailure"));
                }
            }
            
        }
         private void BrowseBack_CanExecute(object sender, CanExecuteRoutedEventArgs e)
         {
             try
             {
                 e.CanExecute = wbBrowser.CanGoBack;
             }
             catch
             {
                 e.CanExecute = false;
             }
             finally
             {
                 e.Handled = true;
             }
         }
         private void BrowseBack_Executed(object sender, ExecutedRoutedEventArgs e)
         {
             try
             {
                 wbBrowser.GoBack();
             }
             catch
             {

             }
             finally
             {
                 e.Handled = true;
             }
         }
         private void BrowseForward_CanExecute(object sender, CanExecuteRoutedEventArgs e)
         {
             try
             {
                 e.CanExecute = wbBrowser.CanGoForward;
             }
             catch
             {
                 e.CanExecute = false;
             }
             finally
             {
                 e.Handled = true;
             }
         }
         private void BrowseForward_Executed(object sender, ExecutedRoutedEventArgs e)
         {
             try
             {
                 wbBrowser.GoForward();
             }
             catch
             {

             }
             finally
             {
                 e.Handled = true;
             }
         }
         private void Refresh_CanExecute(object sender, CanExecuteRoutedEventArgs e)
         {
             e.CanExecute = true;
             e.Handled = true;
         }
         private void Refresh_Executed(object sender, ExecutedRoutedEventArgs e)
         {
             try
             {
                 wbBrowser.Refresh();
             }
             catch
             {

             }
             finally
             {
                 e.Handled = true;
             }
         }
         private void Stop_CanExecute(object sender, CanExecuteRoutedEventArgs e)
         {
             e.CanExecute = true;
             e.Handled = true;
         }
         private void Stop_Executed(object sender, ExecutedRoutedEventArgs e)
         {
             try
             {
                 
             }
             catch
             {

             }
             finally
             {
                 e.Handled = true;
             }
         }
	}
}